package com.newlink.building.userinfo.utils;


import android.Manifest;
import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.ActivityCompat;


import java.lang.reflect.Method;

/**
 * @Author: Jake
 * @Date: 2024-03-22
 * @Description:
 */
public final class Module_UserInfo_PermitUtils {
    private Module_UserInfo_PermitUtils() {
    }

    /**
     * 判断是否有定位权限
     *
     * @param context 上下文
     * @return 是否有定位权限
     */
    public static boolean hasLocationPermission(Context context) {
        return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 打开APP设置界面
     *
     * @param context 上下文
     */
    public static void startAppSettingActivity(Context context) {
        Intent mIntent = new Intent();
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        mIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        context.startActivity(mIntent);
//		ToastUtils.showLong("请授予锁屏显示和后台弹出界面权限");
    }

    //------------------miui-----------------------

    /**
     * @param context 上下文
     * @return MIUI是否可以从后台启动
     */
    public static boolean canStartFromBackground(Context context) {
        AppOpsManager ops = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            int op = 10021;
            // >= 23
            // ops.checkOpNoThrow(op, uid, packageName)
            Method method = ops.getClass().getMethod("checkOpNoThrow", int.class, int.class, String.class);
            Integer result = (Integer) method.invoke(ops, op, android.os.Process.myUid(), context.getPackageName());
            return result == AppOpsManager.MODE_ALLOWED;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * @param context 上下文
     * @return MIUI是否可以锁屏显示
     */
    public static boolean canShowOnLockScreen(Context context) {
        AppOpsManager ops = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            int op = 10020;
            // >= 23
            // ops.checkOpNoThrow(op, uid, packageName)
            Method method = ops.getClass().getMethod("checkOpNoThrow", int.class, int.class, String.class);
            Integer result = (Integer) method.invoke(ops, op, android.os.Process.myUid(), context.getPackageName());
            return result == AppOpsManager.MODE_ALLOWED;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 申请悬浮窗权限
     *
     * @param activity
     */
    public static void requestOverDrawPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(activity)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + activity.getPackageName()));
            activity.startActivityForResult(intent, 0);
//			ToastUtils.showLong("请授予悬浮窗权限");
        }
    }

    /**
     * 检查呼叫权限
     */
    public static void checkCallPermission(Activity activity) {
        boolean canStartFromBackground = canStartFromBackground(activity);
        boolean canShowOnLockScreen = canShowOnLockScreen(activity);
        if (!canStartFromBackground || !canShowOnLockScreen) {
            startAppSettingActivity(activity);
            return;
        }

        requestOverDrawPermission(activity);
    }
}
